//
//  XHF_HomeViewController.m
//  haojia
//
//  Created by 徐宏飞 on 16/4/11.
//  Copyright © 2016年 徐宏飞. All rights reserved.
//

#import "XHF_HomeViewController.h"

#import "XHF_HostViewController.h"
#import "XHF_DesignerViewController.h"
#import "XHF_ElectricianViewController.h"
#import "XHF_WetViewController.h"
#import "XHF_WoodViewController.h"
#import "XHF_PainterViewController.h"


static CGFloat const titleH = 44;
static CGFloat const navBarH = 64;
static CGFloat const tabBarH = 89;
static CGFloat const maxTitleScale = 1.2;


@interface XHF_HomeViewController ()<UIScrollViewDelegate>

@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, weak) UIScrollView *contentScrollView;
@property (nonatomic, weak) UIView *sliderView;

// 选中按钮
@property (nonatomic, weak) UIButton *selTitleButton;
@property (nonatomic, strong) NSMutableArray *buttons;

@end

@implementation XHF_HomeViewController


- (NSMutableArray *)buttons
{
    if (!_buttons)
    {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    
    [self setupTitleScrollView];//创建小scrollview
    [self setupContentScrollView];//创建大scrollview
    [self setupSliderScrollView];//设置小条
    [self addChildViewController];//添加子控制器
    [self setupTitle];//根据子视图个数创建小scrollview的按钮

    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count * SCREEN_WIDTH, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.delegate = self;
    
    
    
}

#pragma mark - 设置头部标题栏
- (void)setupTitleScrollView
{
    // 判断是否存在导航控制器来判断y值
//    CGFloat y = self.navigationController ? navBarH : 0;
    CGFloat y = 0;
    CGRect rect = CGRectMake(0, y, SCREEN_WIDTH, titleH);
    
    UIScrollView *titleScrollView = [[UIScrollView alloc] initWithFrame:rect];
    titleScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleScrollView];
    
    self.titleScrollView = titleScrollView;
}


#pragma mark - 设置内容
- (void)setupContentScrollView
{
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect = CGRectMake(0, y, SCREEN_WIDTH, SCREEN_HEIGHT - navBarH - tabBarH-titleH);
    
    UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:rect];
    //    contentScrollView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:contentScrollView];
    
    self.contentScrollView = contentScrollView;
}

#pragma mark - 设置小条
- (void)setupSliderScrollView{
    
    CGRect rect = CGRectMake(0, 42, 70, 2);
    UIView *sliderView = [[UIView alloc] initWithFrame:rect];
    sliderView.backgroundColor = COLOR_SELECTED;
    [self.titleScrollView addSubview:sliderView];
    self.sliderView = sliderView;
    
}

#pragma mark - 添加子控制器
- (void)addChildViewController
{
    
    XHF_HostViewController *hostVC = [[XHF_HostViewController alloc] init];
    hostVC.title = @"首页";
    [self addChildViewController:hostVC];
    
    
    XHF_DesignerViewController *designVC = [[XHF_DesignerViewController alloc] init];
    designVC.title = @"设计师";
    [self addChildViewController:designVC];
    
    
    XHF_ElectricianViewController *electricianVC = [[XHF_ElectricianViewController alloc] init];
    electricianVC.title = @"水电工";
    [self addChildViewController:electricianVC];
    
    
    XHF_WetViewController *wetVC = [[XHF_WetViewController alloc] init];
    wetVC.title = @"泥工";
    [self addChildViewController:wetVC];
    
    
    XHF_WoodViewController *woodVC = [[XHF_WoodViewController alloc] init];
    woodVC.title = @"木工";
    [self addChildViewController:woodVC];
    
    
    XHF_PainterViewController *painterVC = [[XHF_PainterViewController alloc] init];
    painterVC.title = @"油漆工";
    [self addChildViewController:painterVC];
    
}

#pragma mark - 设置标题区
- (void)setupTitle
{
    NSUInteger count = self.childViewControllers.count;
    
    CGFloat x = 0;
    CGFloat w = 70;
    CGFloat h = titleH;
    
    for (int i = 0; i < count; i++)
    {
        
        UIViewController *vc = self.childViewControllers[i];
        x = i * w;
        CGRect rect = CGRectMake(x, 0, w, h);
        UIButton *btn = [[UIButton alloc] initWithFrame:rect];
        
        btn.tag = i;
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        
        [btn addTarget:self action:@selector(chick:) forControlEvents:UIControlEventTouchDown];
        
        [self.buttons addObject:btn];
        [self.titleScrollView addSubview:btn];
        
        if (i == 0)
        {
            [self chick:btn];
        }
        
    }
    self.titleScrollView.contentSize = CGSizeMake(count * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
}

#pragma mark -  按钮点击
- (void)chick:(UIButton *)btn
{
    [self selTitleBtn:btn];
    
    NSUInteger i = btn.tag;
    CGFloat x = i * SCREEN_WIDTH;
    
    [self setUpOneChildViewController:i];
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
    
}

#pragma mark -  选中按钮
- (void)selTitleBtn:(UIButton *)btn
{
    [self.selTitleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.selTitleButton.transform = CGAffineTransformIdentity;
    
    [btn setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
    btn.transform = CGAffineTransformMakeScale(maxTitleScale, maxTitleScale);
    
    self.selTitleButton = btn;
    [self setupTitleCenter:btn];
}


- (void)setUpOneChildViewController:(NSUInteger)i
{
    CGFloat x = i * SCREEN_WIDTH;
    
    UIViewController *vc = self.childViewControllers[i];
    
    if (vc.view.superview) {
        return;
    }
    vc.view.frame = CGRectMake(x, 0, SCREEN_WIDTH, SCREEN_HEIGHT - self.contentScrollView.frame.origin.y);
    
    [self.contentScrollView addSubview:vc.view];
    
}


- (void)setupTitleCenter:(UIButton *)btn
{
    CGFloat offset = btn.center.x - SCREEN_WIDTH * 0.5;
    
    if (offset < 0)
    {
        offset = 0;
    }
    
    CGFloat maxOffset = self.titleScrollView.contentSize.width - SCREEN_WIDTH;
    if (offset > maxOffset)
    {
        offset = maxOffset;
    }
    [self.titleScrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
    
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSUInteger i = self.contentScrollView.contentOffset.x / SCREEN_WIDTH;
    [self selTitleBtn:self.buttons[i]];
    [self setUpOneChildViewController:i];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offsetX = scrollView.contentOffset.x;
    
    self.sliderView.frame = CGRectMake(offsetX / SCREEN_WIDTH *70, 42, 70, 2);
    
    
    NSInteger leftIndex = offsetX / SCREEN_WIDTH;
    NSInteger rightIndex = leftIndex + 1;
    
//        NSLog(@"%zd,%zd",leftIndex,rightIndex);
    
    UIButton *leftButton = self.buttons[leftIndex];
    
    UIButton *rightButton = nil;
    if (rightIndex < self.buttons.count) {
        rightButton = self.buttons[rightIndex];
    }
    
    CGFloat scaleR = offsetX / SCREEN_WIDTH - leftIndex;
    CGFloat scaleL = 1 - scaleR;
    
    CGFloat transScale = maxTitleScale - 1;
    leftButton.transform = CGAffineTransformMakeScale(scaleL * transScale + 1, scaleL * transScale + 1);
    
    rightButton.transform = CGAffineTransformMakeScale(scaleR * transScale + 1, scaleR * transScale + 1);
    
//    NSLog(@"L%lf R%lf", scaleL, scaleR);
//    UIColor *rightColor = [UIColor colorWithRed:0 green:scaleR blue:0 alpha:1];
//    UIColor *leftColor = [UIColor colorWithRed:0 green:scaleL blue:0 alpha:1];
//    
//    [leftButton setTitleColor:leftColor forState:UIControlStateNormal];
//    [rightButton setTitleColor:rightColor forState:UIControlStateNormal];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
